package by.grsu.coctails.fragments;

import java.util.ArrayList;
import java.util.List;

import com.google.android.imageloader.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import by.grsu.coctails.R;
import by.grsu.coctails.adapter.IngredientAdapter;
import by.grsu.coctails.common.utils.ImageManager;
import by.grsu.coctails.constants.ApplicationIntents;
import by.grsu.coctails.model.Ingredient;

public class AddCoctailFragment extends CommonReceiverFragment {

	private ListView mList;

	private LinearLayout bottomView;

	private IngredientAdapter mAdapter;

	private List<Ingredient> mIngredientList;

	private Button mAddButton;

	private Button mSaveButton;

	private ImageView mImage;

	private static final int IMAGE_PICK_ACTIVITY_REQUEST_CODE = 1598;

	private ImageLoader mImageLoader;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mImageLoader = ImageLoader.get(getActivity());

		mList = (ListView) getView().findViewById(R.id.list);
		bottomView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(
				R.layout.add_coctail_bottom_button, null, false);
		mIngredientList = new ArrayList<Ingredient>();
		mAddButton = (Button) bottomView.findViewById(R.id.add);
		mSaveButton = (Button) bottomView.findViewById(R.id.save);
		mImage = (ImageView) getView().findViewById(R.id.image);
		mImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent galleryIntent = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//				galleryIntent.setType("image/*");
				getActivity().startActivityForResult(galleryIntent,
						IMAGE_PICK_ACTIVITY_REQUEST_CODE);
			}
		});
		mAddButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onAddNewIngredientClick();
			}
		});

		mSaveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		mAdapter = new IngredientAdapter(getActivity(),
				R.layout.ingredient_item, mIngredientList);
		mList.addFooterView(bottomView);
		mList.setAdapter(mAdapter);
		registerReceiver();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.add_coctail, container, false);
	}

	private void onAddNewIngredientClick() {
		FragmentCreator.addAllIngredientFragment(getActivity());
	}

//	@Override
//	protected void onReceived(Context context, Intent intent) {
//		if (intent.getAction().equals(
//				ApplicationIntents.INGREDIENT_SELECTED_INTENT)) {
//			Ingredient ingredient = intent
//					.getParcelableExtra(ApplicationIntents.INGREDIENT_SELECTED_INTENT_EXTRA);
//			mAdapter.add(ingredient);
//			mAdapter.notifyDataSetChanged();
//		}
//
//		if (intent.getAction().equals(ApplicationIntents.SELECT_IMAGE_INTENT)) {
//			String path = intent
//					.getStringExtra(ApplicationIntents.SELECT_IMAGE_EXTRA);
//			mImageLoader.bind(mImage, ImageManager.HTTP + path, null);
//		}
//	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

//	@Override
//	protected IntentFilter getIntentFilter() {
//		IntentFilter filter = new IntentFilter();
//		filter.addAction(ApplicationIntents.INGREDIENT_SELECTED_INTENT);
//		filter.addAction(ApplicationIntents.SELECT_IMAGE_INTENT);
//		return filter;
//	}

	@Override
	public void onStop() {
		super.onStop();
		unRegisterReceiver();
	}

}
